import axios from 'axios'
export const baseUrl = 'http://120.76.247.5:2003'

import store from '@/store'
// const {userInfo} = store.getState()

const request = axios.create({
    baseURL:baseUrl + '/api',

    // bug:直接写在axios.create中的请求头数据无法实现更新
    // 解决方案：axios请求拦截
    // headers:{
    //     Authorization: userInfo.authorization
    // },
})

// 请求拦截：请求时才获取数据的方式
request.interceptors.request.use((config)=>{
    // 给增删改添加Authorization请求头
    if(['post','delete','put','patch'].includes(config.method)){
        const {user} = store.getState()
        config.headers.Authorization = user.userInfo.authorization

    }
    return config;
})


export default request
